package com.neusoft.ysu.populationmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neusoft.ysu.populationmanagement.constant.Errors;
import com.neusoft.ysu.populationmanagement.constant.TabelDic;
import com.neusoft.ysu.populationmanagement.entity.CommonResult;
import com.neusoft.ysu.populationmanagement.entity.CommonResult;
import com.neusoft.ysu.populationmanagement.entity.IdCard;
import com.neusoft.ysu.populationmanagement.mapper.IdCardMapper;
import com.neusoft.ysu.populationmanagement.service.IIdCardService;
import com.neusoft.ysu.populationmanagement.service.IOperationLogService;
import com.neusoft.ysu.populationmanagement.service.IPopulationService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者：赵子鉴
 * @since 2024-06-24
 */
@Service
public class IdCardServiceImpl  implements IIdCardService {

    @Autowired
    IdCardMapper idCardMapper;
    @Autowired
    IOperationLogService logService;
    @Autowired
    IPopulationService populationService;
    @Override
    public CommonResult apllyIdCard(String idnum, Long pid, Long operatorId) {
        // 检查是否存在人口
        if (populationService.selectByIdNum(idnum).getCode() == Errors.ERROR) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该人未录入", null);
        }
        // 检查是否有未作废的身份证
        List<IdCard> idCards = selectIdCardByIdnum(idnum);
        if (!idCards.isEmpty()) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民已有未作废的身份证", idCards);
        }

        // 新增身份证
        IdCard idCard = new IdCard();
        idCard.setIdnum(idnum);
        idCard.setAuditStatus(TabelDic.APPLY_FINISHED);
        idCard.setApllyTime(LocalDateTime.now());
        idCard.setPid(pid);
        idCard.setOperatorId(operatorId);
        idCard.setCreateTime(LocalDateTime.now());
        idCard.setIsDelete(TabelDic.IS_DELETE_NO);
        int result = idCardMapper.insert(idCard);
        if (result == 0) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "新增身份证失败(数据库插入失败)", idCard);
        }

        // 新增日志记录
        CommonResult log = logService.insertByIdnum(idnum, pid, operatorId, "申请身份证", "公安干警操作");
        if (log.getCode() == Errors.ERROR) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "新增身份证成功，新增日志记录失败", log);
        }
        return new CommonResult(Errors.SUCCESS, "新增身份证成功", idCard);

    }

    @Override
    public CommonResult buildIdCard(String idnum, Long pid, Long operatorId) {
        List<IdCard> idCardList = selectIdCardByIdnum(idnum);
        if (idCardList.isEmpty()) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民未申领身份证", null);
        }
        if (idCardList.size() != 1) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民有多个未作废的身份证", null);
        }
        IdCard idCard = idCardList.get(0);
        if (!idCard.getAuditStatus().equals(TabelDic.APPLY_FINISHED)) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民身份证处于 " + idCard.getAuditStatus() + " 状态，请重新申请", idCard);
        }
        idCard.setAuditStatus(TabelDic.BUILDING);
        idCard.setBuildTime(LocalDateTime.now());
        return new CommonResult<>(1,"制证成功",idCardMapper.update(idCard,new LambdaQueryWrapper<IdCard>().eq(IdCard::getId,idCard.getId())) );
    }

    @Override
    public CommonResult finishIdCard(String idnum, Long pid, Long operatorId) {
        List<IdCard> idCardList = selectIdCardByIdnum(idnum);
        if (idCardList.isEmpty()) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民未申领身份证", null);
        }
        if (idCardList.size() != 1) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民有多个未作废的身份证", null);
        }
        IdCard idCard = idCardList.get(0);
        if (!idCard.getAuditStatus().equals(TabelDic.BUILDING)) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民身份证处于 " + idCard.getAuditStatus() + " 状态，请重新申请", idCard);
        }
        idCard.setAuditStatus(TabelDic.BUILDI_FINISHED);
        idCard.setFinishTime(LocalDateTime.now());
        return new CommonResult<>(1,"完证成功",idCardMapper.update(idCard,new LambdaQueryWrapper<IdCard>().eq(IdCard::getId,idCard.getId())) );

    }

    @Override
    public CommonResult sendIdCard(String idnum, Long pid, Long operatorId) {
        List<IdCard> idCardList = selectIdCardByIdnum(idnum);
        if (idCardList.isEmpty()) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民未申领身份证", null);
        }
        if (idCardList.size() != 1) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民有多个未作废的身份证", null);
        }
        IdCard idCard = idCardList.get(0);
        if (!idCard.getAuditStatus().equals(TabelDic.BUILDI_FINISHED)) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民身份证处于 " + idCard.getAuditStatus() + " 状态，请重新申请", idCard);
        }
        idCard.setAuditStatus(TabelDic.SEND_FINISHED);
        idCard.setSendTime(LocalDateTime.now());
        return new CommonResult<>(1,"发证成功",idCardMapper.update(idCard,new LambdaQueryWrapper<IdCard>().eq(IdCard::getId,idCard.getId())) );

    }

    @Override
    public CommonResult lossIdCard(String idnum, Long pid, Long operatorId) {
        List<IdCard> idCardList = selectIdCardByIdnum(idnum);
        if (idCardList.isEmpty()) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民未申领身份证", null);
        }
        if (idCardList.size() != 1) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民有多个未作废的身份证", null);
        }
        IdCard idCard = idCardList.get(0);
        if (!idCard.getAuditStatus().equals(TabelDic.SEND_FINISHED)) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民身份证处于 " + idCard.getAuditStatus() + " 状态，请重新申请", idCard);
        }
        idCard.setAuditStatus(TabelDic.LOSS);
        idCard.setLossTime(LocalDateTime.now());
        return new CommonResult<>(1,"身份证已挂失",idCardMapper.update(idCard,new LambdaQueryWrapper<IdCard>().eq(IdCard::getId,idCard.getId())) );

    }

    @Override
    public CommonResult deleteIdCard(String idnum, Long pid, Long operatorId) {
        List<IdCard> idCardList = selectIdCardByIdnum(idnum);
        if (idCardList.isEmpty()) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民未申领身份证", null);
        }
        if (idCardList.size() != 1) {
            return new CommonResult(Errors.ERROR_RES_IDCARD, "该居民有多个未作废的身份证", null);
        }
        IdCard idCard = idCardList.get(0);

        idCard.setAuditStatus(TabelDic.BUILDI_FINISHED);
        idCard.setValidEndTime(LocalDateTime.now());
        idCard.setIsDelete(TabelDic.IS_DELETE_YES);

        return new CommonResult<>(1,"身份证已作废",idCardMapper.update(idCard,new LambdaQueryWrapper<IdCard>().eq(IdCard::getId,idCard.getId())) );

    }

    @Override
    public List<IdCard> selectIdCardByIdnum(String idnum) {
        LambdaQueryWrapper<IdCard> lq = new LambdaQueryWrapper<>();
        lq.eq(IdCard::getIdnum, idnum);
        lq.eq(IdCard::getIsDelete, TabelDic.IS_DELETE_NO);
        List<IdCard> idCardList = idCardMapper.selectList(lq);
        return idCardList;
    }

}
